package com.zkthink.ceres.pay.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by kellen on 2020/5/3. 微信小程序服务配置
 */
@Configuration
public class WxMaConfiguration {

    @Value("${ceres.wx.ma.appid}")
    private String appid;

    @Value("${ceres.wx.ma.secret}")
    private String secret;

    @Value("${ceres.wx.ma.mch-id}")
    private String mchId;

    @Value("${ceres.wx.ma.mch-key}")
    private String mchKey;

    @Value("${ceres.wx.ma.notify-url}")
    private String notifyUrl;

    @Value("${ceres.wx.ma.key-path}")
    private String keyPath;
    public String getAppid() {
        return appid;
    }
    /**
     * 退款通知url
     */
    @Value("${ceres.wx.ma.refund-notify-url:http://ceres.zkthink.com/app/notify/refund}")
    private String refundNotifyUrl;

    @Bean
    public WxMaService wxMaService() {
        WxMaService wxMaService = new WxMaServiceImpl();
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(appid);
        config.setSecret(secret);
        wxMaService.setWxMaConfig(config);
        return wxMaService;
    }

    @Bean
    public WxPayConfig wxPayConfig() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(appid);
        payConfig.setMchId(mchId);
        payConfig.setMchKey(mchKey);
        payConfig.setNotifyUrl(notifyUrl);
        payConfig.setKeyPath(keyPath);
        payConfig.setTradeType("JSAPI");
        payConfig.setSignType("SHA256withRSA");
        return payConfig;
    }

    @Bean
    public WxPayService wxPayService(WxPayConfig wxPayConfig) {
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(wxPayConfig);
        return wxPayService;
    }


}
